//
//  BBHomeViewController.m
//  QuLuoMianApp
//
//  Created by zyz on 2017/12/19.
//  Copyright © 2017年 科大讯飞. All rights reserved.
//

#import "BBHomeViewController.h"
#import "BBSpaceViewController.h"
#import "HomeSubGridViewController.h"
#import "HomeSubGrid2ViewController.h"
#import "HomeSubGrid3ViewController.h"
#import "HomeSubGrid4ViewController.h"
#import "HomeSubGrid5ViewController.h"
#import "HomeSubGrid6ViewController.h"
#import "HomeSubGrid7ViewController.h"
#import "HomeSubGrid8ViewController.h"

#import "HomeSubList1ViewController.h"
#import "HomeSubList2ViewController.h"
#import "HomeSubList3ViewController.h"
#import "HomeSubList4ViewController.h"


#import "HomeAdsCell.h"

#import "HomeSearchHeadView.h"
#import "DCGoodsGridCell.h"      //8个选项

#import "HomeListSectionHeadView.h"
#import "HomeMainListCell.h"

#import "BBHomeElseReusableView.h"
#import "BBHomeElseReusableView.h"

#import "DCGridItem.h"
#import "HomeListModel.h"

static NSString *const DCHomeAdsCellID = @"DCHomeAdsCellID";
static NSString *const DCGoodsGridCellID = @"DCGoodsGridCell";
static NSString *const DCHomeMainListCellID = @"DCHomeMainListCellID";

/* head */
static NSString *const kBBHomeSearchHeadViewID = @"kBBHomeSearchHeadViewID";
static NSString *const kBBHomeListSectionHeadViewID = @"kBBHomeListSectionHeadViewID";

//footer
static NSString * const kBBHomeElseReusableViewID = @"kBBHomeElseReusableViewID";

@interface BBHomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,SDCycleScrollViewDelegate>

/*控件*/
@property (strong , nonatomic)UICollectionView *m_collectionView;
/* 10个属性 */
@property (strong , nonatomic)NSMutableArray<DCGridItem *> *gridItem;

@property (strong , nonatomic)NSMutableArray * dataList;

@end

@implementation BBHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    [self setUpGoodsData];
     [self.view addSubview:self.m_collectionView];
}



#pragma mark - 加载数据
- (void)setUpGoodsData
{
    _gridItem = [DCGridItem mj_objectArrayWithFilename:@"GoodsGrid.plist"];
    
    NSArray * tempData = @[
                             @{@"bgIcon":@"home_list_bg1_icon",@"btnIcon":@"home_list_btn1_icon"},
                             @{@"bgIcon":@"home_list_bg2_icon",@"btnIcon":@"home_list_btn2_icon"},
                             @{@"bgIcon":@"home_list_bg3_icon",@"btnIcon":@"home_list_btn3_icon"},
                             @{@"bgIcon":@"home_list_bg4_icon",@"btnIcon":@"home_list_btn4_icon"}
                           ];
    
    _dataList = [HomeListModel mj_objectArrayWithKeyValuesArray:tempData];
    
    
}

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    
    [self.m_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    
}

#pragma mark --
#pragma mark -- collectionView 代理
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return _gridItem.count;;
    }else{
        return self.dataList.count;
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        HomeAdsCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:DCHomeAdsCellID forIndexPath:indexPath];
        return cell;
    }
    else if (indexPath.section == 1){
        DCGoodsGridCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:DCGoodsGridCellID forIndexPath:indexPath];
         cell.gridItem = _gridItem[indexPath.row];
        return cell;
    }
    else{
        HomeMainListCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:DCHomeMainListCellID forIndexPath:indexPath];
        cell.listModel = self.dataList[indexPath.item];
        
        cell.btnIcon.hidden = (indexPath.item % 2 == 0) ? NO : YES;
        cell.leftBtnIcon.hidden = (indexPath.item % 2 == 0) ? YES : NO;
        
        cell.btnBlock = ^{
          
            switch (indexPath.row) {
                case 0:
                {
                    HomeSubList1ViewController * vc = [HomeSubList1ViewController new];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 1:
                {
                    HomeSubList2ViewController * vc = [HomeSubList2ViewController new];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 2:
                {
                    HomeSubList3ViewController * vc = [HomeSubList3ViewController new];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 3:
                {
                    HomeSubList4ViewController * vc = [HomeSubList4ViewController new];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                    
                default:
                    break;
            }
            
        };
        
        return cell;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if (kind == UICollectionElementKindSectionHeader){
        
        if (indexPath.section == 1) {
            HomeSearchHeadView * headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kBBHomeSearchHeadViewID forIndexPath:indexPath];
            
            return headView;
            
        }else if (indexPath.section == 2){
            HomeListSectionHeadView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kBBHomeListSectionHeadViewID forIndexPath:indexPath];
            
     
            
            return headerView;
            
        }
        else{
//             return [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kBBHomeElseReusableViewID forIndexPath:indexPath];
        }
    }
    
    
    if (kind == UICollectionElementKindSectionFooter) {
        
      
    }
    
     return [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kBBHomeElseReusableViewID forIndexPath:indexPath];

}

#pragma mark - item宽高
- (CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
         return CGSizeMake(kScreenWidth, 150); //图片滚动的宽高
       
    }else if (indexPath.section == 1) {
        return CGSizeMake((kScreenWidth - 50 )/4 , (kScreenWidth - 50 )/4 - 5);
        
    }else{
        return CGSizeMake(kScreenWidth , 142);
    }
    
    

}

#pragma mark - head宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
   
    if (section == 1) {
        return CGSizeMake(kScreenWidth, 50);
    }
    if (section == 2) {
        return CGSizeMake(kScreenWidth, 50);
    }
    return CGSizeZero;
}

#pragma mark - foot宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        
//        BBSpaceViewController * spaceVC = [[BBSpaceViewController alloc] init];
//        [self.navigationController pushViewController:spaceVC animated:YES];
        
        switch (indexPath.item) {
            case 0:
            {
                HomeSubGridViewController * VC = [[HomeSubGridViewController alloc] init];
               [self.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 1:
            {
                HomeSubGrid2ViewController * VC = [[HomeSubGrid2ViewController alloc] init];
                [self.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 2:
            {
                HomeSubGrid3ViewController * VC = [[HomeSubGrid3ViewController alloc] init];
                [self.navigationController pushViewController:VC animated:YES];
            }
                
                break;
            case 3:
            {
                HomeSubGrid4ViewController * VC = [[HomeSubGrid4ViewController alloc] init];
                [self.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 4:
            {
                HomeSubGrid5ViewController * VC = [[HomeSubGrid5ViewController alloc] init];
                [self.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 5:
            {
                HomeSubGrid6ViewController * VC = [[HomeSubGrid6ViewController alloc] init];
                [self.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 6:
            {
                HomeSubGrid7ViewController * VC = [[HomeSubGrid7ViewController alloc] init];
                [self.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 7:
            {
                HomeSubGrid8ViewController * VC = [[HomeSubGrid8ViewController alloc] init];
                [self.navigationController pushViewController:VC animated:YES];
            }
                break;
                
            default:
                break;
        }
    }
    
    
}

#pragma mark --
#pragma mark -- lazy

- (UICollectionView *)m_collectionView{
    if(!_m_collectionView){
        
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
        _m_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _m_collectionView.delegate = self;
        _m_collectionView.dataSource = self;
        _m_collectionView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight  - 49 - 64);
        _m_collectionView.showsVerticalScrollIndicator = NO;
        _m_collectionView.backgroundColor = [UIColor whiteColor];
        
        //注册 section 0
        [_m_collectionView registerClass:[HomeAdsCell class] forCellWithReuseIdentifier:DCHomeAdsCellID];
       
        
        // section1
          [_m_collectionView registerNib:[UINib nibWithNibName:@"HomeSearchHeadView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kBBHomeSearchHeadViewID];
        [_m_collectionView registerClass:[DCGoodsGridCell class] forCellWithReuseIdentifier:DCGoodsGridCellID];
        
        
        // section2
            [_m_collectionView registerNib:[UINib nibWithNibName:@"HomeListSectionHeadView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kBBHomeListSectionHeadViewID];
        
        [_m_collectionView registerNib:[UINib nibWithNibName:@"HomeMainListCell" bundle:nil] forCellWithReuseIdentifier:DCHomeMainListCellID];
        
        
          [_m_collectionView registerClass:[BBHomeElseReusableView class]  forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kBBHomeElseReusableViewID];
 
        
    }
    return _m_collectionView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
